{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fb27b941602401d91542211134fc71a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import CDIndexOption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import HistoricalPricingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acae54e37e7d407bbb7b55eff062a284",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a63283cbaf04dbcab1f6479b197f3a8",
   "metadata": {},
   "source": [
    "Here we price a payer spread on the iTraxx Crossover OTR index\n",
    "\n",
    "We buy a XOVER call and sell an offsetting XOVER call with a higher strike"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dd0d8092fe74a7c96281538738b07e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Buy 3m XO call k=300\n",
    "long_leg = CDIndexOption(\n",
    "    index_family='iTraxx Europe XOVER',\n",
    "    strike=0.0300,\n",
    "    option_type='Call',\n",
    "    expiration_date='3m',\n",
    "    termination_date='5y',\n",
    "    buy_sell='Buy',\n",
    "    name='Long_XOVER_Call',\n",
    ")\n",
    "# Sell 3m XO call k=350\n",
    "short_leg = CDIndexOption(\n",
    "    index_family='iTraxx Europe XOVER',\n",
    "    strike=0.0350,\n",
    "    option_type='Call',\n",
    "    expiration_date='3m',\n",
    "    termination_date='5y',\n",
    "    buy_sell='Sell',\n",
    "    name='Short_XOVER_Call',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72eea5119410473aa328ad9291626812",
   "metadata": {},
   "outputs": [],
   "source": [
    "payer_spread = Portfolio((long_leg, short_leg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8edb47106e1a46a883d545849b8ab81b",
   "metadata": {
    "tags": [
     "Contexts - Historical Pricing Context"
    ]
   },
   "outputs": [],
   "source": [
    "with HistoricalPricingContext(10, show_progress=True):\n",
    "    prices = payer_spread.price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10185d26023b46108eb7d9f57d49d2b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "prices.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8763a12b2bbd4a93a75aff182afb95dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot both legs of the payer spraed as well as the overall PV\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(prices.to_frame()['Long_XOVER_Call'], label='Buy leg, k=300')\n",
    "ax.plot(prices.to_frame()['Short_XOVER_Call'], label='Sell leg, k=350')\n",
    "ax.plot(prices.aggregate(), label='Overall cost')\n",
    "ax.set_xlabel('time')\n",
    "ax.set_ylabel('PV ($)')\n",
    "ax.set_title('PV over Time')\n",
    "ax.legend(bbox_to_anchor=(1, 0.5))\n",
    "fig.autofmt_xdate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7623eae2785240b9bd12b16a66d81610",
   "metadata": {},
   "outputs": [],
   "source": [
    "prices.aggregate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "tags": [
   "Instrument - Credit Payer Spread"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
